<template>
  <view>
    <view class="title">答题详情</view>
    <view class="circle">
      <view class="score">
        <view>得分</view>
        <view>{{ exam.score }}</view>
      </view>
    </view>
    <view class="container">
      <view class="ex-success">考试时长：{{ exam.answerInfo?.examTime }}</view>
    </view>
    <!-- 表格布局 -->
    <view class="table">
      <!-- 表头 -->
      <view class="table-header">
        <view class="table-cell">题型</view>
        <view class="table-cell">明细</view>
        <view class="table-cell">对/错</view>
        <view class="table-cell">分数</view>
        <view class="table-cell">正确率</view>
      </view>

      <!-- 数据行 -->
      <view class="table-row" v-for="(item, index) in questionDetails" :key="index">
        <view class="table-cell">{{
          item.type === 1 ? "单选题" : item.type === 2 ? "多选题" : "判断题"
        }}
        </view>
        <view class="table-cell">
          <view>共{{ item.totalNum }}道题</view>
        </view>
        <view class="table-cell">{{ item.rightNum }}/{{ item.wrongNum }}</view>
        <view class="table-cell">{{ item.score }}</view>
        <view class="table-cell">{{ ((item.rightNum / item.totalNum) * 100).toFixed(2) }}%
        </view>
      </view>

      <!-- 分类区域 -->
      <view class="category">
        <view class="category-item">
          <image src="../../assets/错题温习.png" style="width: 45px; height: 45px" />
          <view style="margin-left: 10px">错题温习</view>
          <button class="category-button" @tap="goToExamDetail(0)">查看</button>
        </view>
        <view class="category-item">
          <image src="../../assets/试卷详情.png" style="width: 45px; height: 45px" />
          <view style="margin-left: 10px">试卷详情</view>
          <button class="category-button" @tap="goToExamDetail(1)">查看</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import "./recordDetails.css";
import { onBeforeUnmount, onMounted, ref } from "vue";
import Taro from "@tarojs/taro";
import config from '../../config'


// 题型明细数据
const questionDetails = ref([]);
const exam = ref({});
const unikey = ref('');
// 跳转到查看试卷页面
function goToExamDetail(index) {
  if (index === 0) {
    Taro.setNavigationBarTitle({
      title: "错题温习",
    });
  } else {
    Taro.setNavigationBarTitle({
      title: "试卷详情",
    });
  }
  Taro.navigateTo({
    url: `/pages/reviewWrong/index?index=${index}&unikey=${unikey.value}`,
  });
}
const wxRequest = config.wxPromisify(wx.request)
const data = ref({});
const examId = ref();
const isExam = ref(false);
const questionsTotal = ref(0);
onMounted(() => {
  data.value = Taro.getCurrentInstance().router.params;
  unikey.value = data.value.unikey;
  console.log("unikey:", data.value.unikey);
  // examId.value = data.value.examId;
  // isExam.value = data.value.isExam;
  // questionsTotal.value = data.value.questionsTotal;
  getExamRecord();

});
async function getExamRecord() {
  const res = await wxRequest({
    url: `${config.apiUrl}/exam/findOne/${unikey.value}`,
    method: 'GET',
    header: { 'content-type': 'application/json' }
  })
  exam.value = res.data.data;
  console.log("res:", exam.value);
  questionDetails.value = processQuestionDetails(exam.value);
}
function processQuestionDetails(data) {
  const questions = JSON.parse(data.questions);
  const answerInfo = JSON.parse(data.answerInfo);
  const wrongQuestions = JSON.parse(data.wrongQuestions);
  console.log("answerInfo:", answerInfo);
  console.log("wrongQuestions:", wrongQuestions);
  console.log("questions:", questions);

  // 统计各题型答题详情
  const questionDetails = [];

  // 处理单选题
  if (questions.singleChoice && questions.singleChoice.length > 0) {
    const totalNum = questions.singleChoice.length;
    const wrongNum = JSON.parse(exam.value.answerInfo).singleWrongNum;
    const rightNum = totalNum - wrongNum;
    const score = parseFloat((rightNum * 0.5).toFixed(2)); // 单选题得分
    questionDetails.push({
      type: 1,
      totalNum,
      rightNum,
      wrongNum,
      score
    });
  }

  // 处理多选题
  if (questions.multipleChoice && questions.multipleChoice.length > 0) {
    const totalNum = questions.multipleChoice.length;
    const wrongNum = JSON.parse(exam.value.answerInfo).multiWrongNum;
    const rightNum = totalNum - wrongNum;
    const score = parseFloat((rightNum * 1).toFixed(2)); // 单选题得分
    questionDetails.push({
      type: 2,
      totalNum,
      rightNum,
      wrongNum,
      score
    });
  }

  // 处理判断题
  if (questions.trueFalse && questions.trueFalse.length > 0) {
    const totalNum = questions.trueFalse.length;
    const wrongNum = JSON.parse(exam.value.answerInfo).judgeWrongNum;
    const rightNum = totalNum - wrongNum;
    const score = parseFloat((rightNum * 1).toFixed(2)); // 单选题得分
    questionDetails.push({
      type: 3,
      totalNum,
      rightNum,
      wrongNum,
      score
    });
  }

  return questionDetails;
}
onBeforeUnmount(() => {
  Promise.resolve().then(() => {
    if (isExam.value) {
      Taro.navigateBack();
    }
  });
});
</script>
<style scoped></style>
